// components/blog-card/blog-card.js
import formatTime from "../../utils/formatTime.js"
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    blog: Object,
    openid: String
  },
  observers: {
    ['blog.createTime'](val) {
      if (val) {
        this.setData({
          _createTime: formatTime(new Date(val))
        })
      }
    },
    ['blog.img'](val) {
      if (val != null) {
        let img = val.sort(function (a, b) {
          var x = "index"
          if (a[x] < b[x]) {
            return -1
          }
          if (a[x] > b[x]) {
            return 1
          }
          return 0
        })
        this.setData({
          img
        })
      }

    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    _createTime: "",
    img: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onPreviewImage(event) {
      let dataset = event.currentTarget.dataset
      let urls = []
      for (let i = 0; i < dataset.imgs.length; i++) {
        urls.push(dataset.imgs[i].img)
      }
      wx.previewImage({
        urls,
        current: dataset.imgsrc
      })
    },
    deleteBlog() {
      wx.showModal({
        title: "是否删除本条博客内容",
        success: (res) => {
          if (res.confirm) {
            wx.showLoading({
              title: '删除中',
            })
            wx.cloud.callFunction({
              name: "blog",
              data: {
                $url: "deleteBlog",
                id: this.properties.blog._id
              }
            }).then((res) => {
              this.triggerEvent("delBlog")
              wx.hideLoading({})
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }

        },


      })

    }
  }
})